/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2008，所有权利保留。
 * 
 * 文件名：	RB_v20r07.java
 * 模块说明：	
 * 修改历史：
 * Feb 20, 2008 - zhangyanbo - 创建。
 */
package com.hd123.rumba.upgrade.v20.r08;

import net.sf.hibernate.HibernateException;

import org.apache.log4j.Logger;

import com.hd123.rumba.cartridge.CartRumba;
import com.hd123.rumba.domain.IDomainMgr;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.upgrade.DBCommand;
import com.hd123.rumba.upgrade.Upgrade;
import com.hd123.rumba.user.CRole;
import com.hd123.rumba.user.CUser;
import com.hd123.rumba.user.IPermissionMgr;
import com.hd123.rumba.user.IRole;
import com.hd123.rumba.user.IRoleMgr;
import com.hd123.rumba.util.Version;

/**
 * 2.0.7.0->2.0.8.0
 * 
 * @author zhangyanbo
 * 
 */
public class RB_v20r07 extends Upgrade {

  public RB_v20r07() throws Exception {
    super();
    baseVersion = new Version("2.0.7.1169");
    targetVersion = new Version("2.0.8.1170");
    cartUuid = this.getCart(CartRumba.class).getUuid();
    logger = Logger.getLogger(this.getClass());
  }

  @Override
  public void executeDdl() throws BusinessException {
    DBCommand cmd = new DBCommand();
    execFile(cmd, "upgrade.sql");
  }

  @Override
  public void executeData() throws BusinessException {
    try {
      initPermissions();
    } catch (HibernateException e) {
      throw new BusinessException(e);
    }
  }

  private void initPermissions() throws HibernateException, BusinessException {
    IRoleMgr roleMgr = (IRoleMgr) NamingMgr.getInstance().lookupMgr(IRoleMgr.class);
    IDomainMgr domainMgr = (IDomainMgr) NamingMgr.getInstance().lookupMgr(IDomainMgr.class);
    IRole adminRole = roleMgr.getRoleByCode(domainMgr.getRootDomain(), CRole.VALUE_CODE_ROOT_ADMIN);

    IPermissionMgr permMgr = (IPermissionMgr) NamingMgr.getInstance().lookupMgr(
        IPermissionMgr.class);
    try {
      permMgr.initPermissionsT(CUser.class);
      permMgr.addPermissionsForRoleT(adminRole, CUser.class);
    } catch (Exception e) {
      if (e instanceof HibernateException)
        throw (HibernateException) e;
      else
        throw new BusinessException(e);
    }
  }
}
